<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class aspek_penilaian extends CI_Controller {
	
	var $objectid = 'aspek_penilaian';
	
	function __construct(){
		parent::__construct();
		$this->load->model('sd/mdl_aspek_penilaian');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data['objectid'] = $this->objectid;
		$this->load->view('sd/aspek_penilaian', $data);
	}
	
	function grid(){
		$data = $this->mdl_aspek_penilaian->get_data();
		echo $this->mdl_aspek_penilaian->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['objectid'] = $this->objectid;
		
		//$data['kd_subaspek_penilaian'] = '';
		$data['kd_aspek']='';
		$data['kd_poin']='';
		$data['nama_poin'] = '';
		$data['keterangan'] = '';
		
		$this->load->view('sd/aspek_penilaian_form', $data);
	}
	
	function edit($kode){
		$data['objectid'] = $this->objectid;
		
		$data['kd_poin'] = $kode;
		$r = $this->mdl_aspek_penilaian->get_data_edit($kode);
		$data['kd_aspek'] = $r->row()->kd_aspek;
		$data['nama_poin'] = $r->row()->nama_poin;
		$data['keterangan'] = $r->row()->keterangan;
		
		$this->load->view('sd/aspek_penilaian_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		//$data['kode_awalan'] = strtoupper($data['kode_awalan']);
		
		# rules validasi form
		$this->form_validation->set_rules('kd_poin', 'Kode Aspek Penilaian', 'trim|required|xss_clean');
		$this->form_validation->set_rules('nama_poin', 'Nama Aspek Penilaian', 'trim|required|xss_clean');
        
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		
		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"].= trim(form_error('kd_poin',' ',' '))==''?'':form_error('kd_poin',' ',' ').'<br>';
			$data["pesan_error"].= trim(form_error('nama_poin',' ',' '))==''?'':form_error('nama_poin',' ',' ').'<br>';
			
		}else{
			if($aksi=="add"){ // add
				$result = !$this->mdl_aspek_penilaian->isExistKode($data['kd_poin']);
				if ($result){
					$result = $this->mdl_aspek_penilaian->insert($data);
				}else{
					$data['pesan_error'] .= 'Sudah terdapat Kode Aspek Penilaian dalam database!';
				}
			}else { // edit
				if($data['kode'] == $data['kd_poin']){ // jika kode tidak diubah
					$result=$this->mdl_aspek_penilaian->update($data);
				}else{
					$result = !$this->mdl_aspek_penilaian->isExistKode($data['kd_poin']);
					if ($result){
						$result=$this->mdl_aspek_penilaian->UpdateOnDb($data);
					}else{
						$data['pesan_error'] .= 'Sudah terdapat Kode Aspek Penilaian dalam database!';
					}
				}
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_aspek_penilaian->delete($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
}